<template>
  <div class="online-stats">
    <el-form
      ref="form"
      :inline="true"
      :model="form"
      class="search-form"
    >
      <el-form-item label="上线时间:">
        <el-date-picker
          v-model="date"
          size="small"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item label="司机姓名:">
        <el-input
          v-model="form.username"
          size="small"
          placeholder="请输入司机姓名"
        ></el-input>
      </el-form-item>
      <el-form-item label="在线状态:">
        <el-select
          v-model="form.online"
          size="small"
          placeholder="在线状态">
          <el-option
            v-for="item in driverOnlineStatus"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <br/>
      <el-form-item label="司机账号:">
        <el-input
          v-model="form.account"
          size="small"
          placeholder="请输入司机账号"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="编号:"
        label-width="135px"
      >
        <el-input
          size="small"
          v-model="form.number"
          placeholder="请输入司机编号"></el-input>
      </el-form-item>
      <el-form-item label-width="70px">
        <template slot="label">
          <div></div>
        </template>
        <el-button
          size="small"
          type="primary"
          @click="search(1)"
        >查询
        </el-button>
      </el-form-item>
    </el-form>
    <div class="operate-bar" style="margin-top: 22px">
      <el-button
        type="primary"
        size="small"
        @click="editAccount({})"
      >
        新增司机账号
      </el-button>
      <!--      <el-button-->
      <!--        size="small"-->
      <!--        @click="exportExcel"-->
      <!--      >导出excel-->
      <!--      </el-button>-->
    </div>
    <el-table
      :data="tableData"
      style="width: 100%"
      v-loading="loading"
      ref="table"
      class="accountManageTable"
    >
      <el-table-column
        prop="number"
        label="编号"
      >
      </el-table-column>
      <el-table-column
        prop="account"
        label="司机账号"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="username"
        label="司机名称"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="phone"
        label="手机号码"
        width="130px"
      >
      </el-table-column>
      <el-table-column
        prop="money"
        label="账号金额"
        width="100px"
      >
      </el-table-column>
      <el-table-column
        prop="state_text"
        label="充值记录"
        width="100px"
      >
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            @click="viewAccountRechargeLog(scope.row)"
          >查看详情
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        prop="state_text"
        label="启用状态"
        width="80px"
      >
      </el-table-column>
      <el-table-column
        prop="create_time"
        label="创建时间"
        width="170px"
      >
      </el-table-column>
      <el-table-column
        label="操作"
        width="325"
      >
        <template slot-scope="scope">
          <el-button
            size="small"
            type="primary"
            @click="editAccount(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            size="small"
            type="success"
            @click="rechargeAccount(scope.row)"
          >
            充值
          </el-button>
          <el-button
            size="small"
            :type="scope.row.state === 1 ? 'danger' : ''"
            @click="toggleAccountState(scope.row)"
          >
            {{scope.row.state === 1 ? '封号' : '启用'}}
          </el-button>
          <el-button
            size="small"
            type="danger"
            @click="unbindPhone(scope.row)"
          >清除手机绑定
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        background
        layout="total, prev, pager, next, jumper"
        :page-size.sync="form.size"
        :current-page.sync="form.page"
        :total="total"
        :hide-on-single-page="true"
        @current-change="search"
      >
      </el-pagination>
    </div>
    <account-recharge></account-recharge>
    <account-recharge-log></account-recharge-log>
    <account-edit></account-edit>
  </div>
</template>

<script>
  /**
   * @Author: lifuzhao
   * @Date: 2019-07-14
   * @Project: driver
   */
  import {pageSize, driverOnlineStatus, driverActivateStatus} from "../../constant"
  import accountRecharge from './parts/recharge'
  import accountRechargeLog from './parts/rechargeLog'
  import accountEdit from './parts/edit'

  export default {
    name: "account-manage"
    , data() {
      return {
        driverOnlineStatus
        , form: {
          page: 1
          , size: pageSize
          , time_begin: ''
          , time_end: ''
          , online: 3
          , username: ''
          , account: '',
          number: ''
        }
        , total: 0
        , loading: false
        , date: []
        , tableData: []
      }
    }
    , watch: {
      date(date) {
        let start = ''
          , end = ''
        if (date) {
          [start, end] = date
        }
        this.form.time_begin = start
        this.form.time_end = end
      }
    }
    , mounted() {
      this.init()
    }
    , methods: {
      init() {
        this.$bus.$on('updateAccountManageList', () => {
          this.getList()
        })
        this.search(1)
      }
      , search(page) {
        this.form.page = page
        this.getList()
      }
      , getList() {
        this.loading = true
        this.$axios.get('/api/v1/drivers', {
          params: this.form
        })
          .then(res => {
            let {total, data} = res
            this.total = total
            let list = data || []
            this.tableData = list.map(item => {
              for (let i = 0; i < driverActivateStatus.length; i++) {
                if (item.state === driverActivateStatus[i].value) {
                  item.state_text = driverActivateStatus[i].label
                  break
                }
              }
              return item
            })
          })
          .finally(() => {
            this.loading = false
          })
      }
      , editAccount(row) {
        this.$bus.$emit('editAccount', row)
      }
      , viewAccountRechargeLog(row) {
        this.$bus.$emit('viewAccountRechargeLog', row)
      }
      , rechargeAccount(record) {
        this.$bus.$emit('rechargeAccount', record)
      }
      , toggleAccountState(record) {
        let title = (record.state === 1 ? '封停' : '启用') + '账号'
        this.$fn.setTitle(title)
        this.$confirm(`是否确认${record.state === 1 ? '封停' : '启用'}账号 ${record.username} (${record.account}) ？`, `${title}提示`)
          .then(() => {
            this.$axios.post('/api/v1/driver/handel', {
              d_id: record.id
              , state: record.state === 1 ? 2 : 1
            })
              .then(res => {
                if (res.success) {
                  this.$message({
                    type: 'success'
                    , message: '操作成功'
                  })
                  this.getList()
                } else {
                  this.$message({
                    type: 'error'
                    , message: '操作失败' + res.msg
                  })
                }
              })
              .catch(() => {
                this.$message({
                  type: 'error'
                  , message: '操作失败'
                })
              })
          })
          .catch(() => {
          })
          .finally(() => {
            this.$fn.setTitle('账号管理')
          })
      }
      , unbindPhone(record) {
        this.$confirm(`是否确认清除 ${record.username} 的手机绑定？`, '清除手机绑定')
          .then(() => {
            this.$axios.post('/api/v1/driver/clearPhoneCode', {
              d_id: record.id
            })
              .then((res) => {
                if (res.success) {
                  this.$message({
                    type: 'success',
                    message: '操作成功'
                  })
                  this.getList()
                } else {
                  this.$message({
                    type: 'error'
                    , message: '操作失败' + res.msg
                  })
                }
              })
              .catch(() => {
                this.$message({
                  type: 'error'
                  , message: '操作失败'
                })
              })
          })
          .catch(() => {
          })
      }

    }
    , components: {
      accountRecharge
      , accountRechargeLog
      , accountEdit
    }
  }
</script>

<style lang="less">
  .online-stats {
    .el-form-item {
      margin-bottom: 12px;
    }
  }

  .accountManageTable {
    .el-table__expand-icon {
      width: 0;
      overflow: hidden;
    }
  }
</style>
